/***************************************************************
*  Copyright (c) 2007 by GroupMe! Team (www.groupme.net)
*  All rights reserved.
*
*  This file is part of the GroupMe! Project. Source code of 
*  this project is closed and redistribution of this code is
*  prohibited. 
*  
*  Contact: http://www.groupme.net
*
*  This copyright notice MUST APPEAR in all copies of the file!
***************************************************************/
package net.groupme.model.dao.mem;

import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;

import net.groupme.exceptions.DeleteFailedException;
import net.groupme.exceptions.IDNotFoundException;
import net.groupme.exceptions.InsertFailedException;
import net.groupme.exceptions.UpdateFailedException;
import net.groupme.gui.constants.VIEW_VOCABULARY;
import net.groupme.model.Group;
import net.groupme.model.MODEL_CONSTANTS;
import net.groupme.model.Resource;
import net.groupme.model.Tag;
import net.groupme.model.User;
import net.groupme.model.dao.ResourceDAO;
import net.groupme.model.rdf.Statement;

/**
 * created on Oct 24, 2007
 * 
 * This class is an In-Memory-DAO implementation of a {@link ResourceDAO}. Hence, nothing is
 * stored within a database. <b>ATTENTION:</b> Functions that require additional
 * infos like {@link Resource#getTags(User)} are not implemented!!
 * 
 * @author Fabian Abel, <a href="mailto:abel@l3s.de">abel@l3s.de</a>
 * @author last edited by: $Author: fabian $
 * 
 * @version $Revision: 1.4 $ $Date: 2008-05-28 09:28:09 $
 */
public class ResourceDAOMemImpl implements ResourceDAO {
	
	private String id = null;
	private String name = null;
	private String description = null;
	private URL url = null;
	private String type = null;
	private Collection<Tag> tags = null;

	/**
	 * Creates a new ResourceDAOMemImpl instance.
	 * @param id the id
	 * @param name the name
	 * @param description the description
	 * @param url url of the resource
	 * @param type type of resource (e.g. group, website,..)
	 */
	public ResourceDAOMemImpl(String id, String name, String description, URL url, String type) {
		super();
		this.id = id;
		this.name = name;
		this.description = description;
		this.url = url;
		this.type = type;
		this.tags = new ArrayList<Tag>();
	}

	/* (non-Javadoc)
	 * @see net.groupme.model.Resource#addStatement(java.lang.String, java.lang.String, java.lang.String)
	 */
	public void addStatement(String property, String object, String datatype)
			throws IDNotFoundException, InsertFailedException {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see net.groupme.model.Resource#addTag(net.groupme.model.User, net.groupme.model.Tag)
	 */
	public void addTag(User tagger, Tag tag) throws InsertFailedException {
		if(!tags.contains(tag)){  //Interesting: If you use a HashSet for tags then this condition is always fulfilled (HashSet.contains(object) does not call the equals method of object!!
			this.tags.add(tag);
		}
	}

	/* (non-Javadoc)
	 * @see net.groupme.model.Resource#addTag(net.groupme.model.User, net.groupme.model.Tag, net.groupme.model.Group)
	 */
	public void addTag(User tagger, Tag tag, Group group)
			throws InsertFailedException {
		if(!tags.contains(tag)){
			this.tags.add(tag);
		}
	}

	/* (non-Javadoc)
	 * @see net.groupme.model.Resource#addTag(net.groupme.model.User, net.groupme.model.Tag, net.groupme.model.Group, java.lang.Double)
	 */
	public void addTag(User tagger, Tag tag, Group group, Double importance)
			throws InsertFailedException {
		if(!tags.contains(tag)){
			this.tags.add(tag);
		}
	}

	/* (non-Javadoc)
	 * @see net.groupme.model.Resource#getDescription()
	 */
	public String getDescription() {
		return this.description;
	}

	/* (non-Javadoc)
	 * @see net.groupme.model.Resource#getGroups()
	 */
	public Collection<Group> getGroups() {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see net.groupme.model.Resource#getName()
	 */
	public String getName() {
		return this.name;
	}

	/* (non-Javadoc)
	 * @see net.groupme.model.Resource#getStatements()
	 */
	public Collection<Statement> getStatements() {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see net.groupme.model.Resource#getTags()
	 */
	public Collection<Tag> getTags() {
		return new HashSet<Tag>(this.tags);
	}

	/* (non-Javadoc)
	 * @see net.groupme.model.Resource#getTags(net.groupme.model.Group)
	 */
	public Collection<Tag> getTags(Group group) {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see net.groupme.model.Resource#getTags(net.groupme.model.User)
	 */
	public Collection<Tag> getTags(User tagger) {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see net.groupme.model.Resource#getTags(net.groupme.model.User, net.groupme.model.Group)
	 */
	public Collection<Tag> getTags(User tagger, Group group) {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see net.groupme.model.Resource#getType()
	 */
	public String getType() {
		return this.type;
	}

	/* (non-Javadoc)
	 * @see net.groupme.model.Resource#getURL()
	 */
	public URL getURL() {
		return this.url;
	}
	
	public String getDateCreated(){
		return null;
	}

	/* (non-Javadoc)
	 * @see net.groupme.model.Resource#removeStatement(net.groupme.model.rdf.Statement)
	 */
	public void removeStatement(Statement statement)
			throws DeleteFailedException {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see net.groupme.model.Resource#removeTag(net.groupme.model.User, net.groupme.model.Tag, net.groupme.model.Group)
	 */
	public void removeTag(User tagger, Tag tag, Group group)
			throws DeleteFailedException {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see net.groupme.model.Resource#setName(java.lang.String)
	 */
	public void setName(String name) throws UpdateFailedException {
		this.name = name;
	}

	/* (non-Javadoc)
	 * @see net.groupme.model.Resource#setType(java.lang.String)
	 */
	public void setType(String type) throws UpdateFailedException {
		this.type = type;
	}

	/* (non-Javadoc)
	 * @see net.groupme.model.Resource#setURL(java.net.URL)
	 */
	public void setURL(URL url) throws UpdateFailedException {
		this.url = url;
	}

	/* (non-Javadoc)
	 * @see net.groupme.model.Identifiable#getId()
	 */
	public String getId() {
		return this.id;
	}

	/* (non-Javadoc)
	 * @see net.groupme.model.Identifiable#getLabel()
	 */
	public String getLabel() {
		return this.name;
	}

	/* (non-Javadoc)
	 * @see net.groupme.model.Identifiable#getURI()
	 */
	public URI getURI() {
		try {
			return new URI((MODEL_CONSTANTS.RESOURCE_TYPE_GROUP.equals(this.type) ? (VIEW_VOCABULARY.RESTful_GROUP_URI_PATTERN + this.id) : (VIEW_VOCABULARY.RESTful_RESOURCE_URI_PATTERN + id)));//$NON-NLS-1$
		} catch (URISyntaxException e) {
			return null;
		}
	}

	/* (non-Javadoc)
	 * @see net.groupme.model.Identifiable#setDescription(java.lang.String)
	 */
	public void setDescription(String description) throws UpdateFailedException {
		this.description = description;
	}

	/* (non-Javadoc)
	 * @see net.groupme.model.Identifiable#setLabel(java.lang.String)
	 */
	public void setLabel(String label) {
		this.name = label;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if(obj instanceof Resource){
			return this.id.equals(((Resource)obj).getId());
		}
		return super.equals(obj);
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		return this.id.hashCode();
	}

}
